﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Xml.Serialization;

namespace Zombie_Demo.User_Interface
{
     [Serializable]
     [XmlRoot("Text")]
     public class Text : Element
     {
          private string text;
          private Color color;
          private string fontKey;

          /// <summary>
          /// The string to be displayed.
          /// </summary>
          public string DisplayText { get { return this.text; } set { this.text = value; } }
          /// <summary>
          /// The color to set the text.
          /// </summary>
          public Color Color { get { return this.color; } set { this.color = value; } }
          /// <summary>
          /// FontKey as stored in the system from LoadFont.
          /// </summary>
          public string FontKey { get { return this.fontKey; } set { this.fontKey = value; } }

          /// <summary>
          /// Create a new default text.
          /// </summary>
          public Text()
               : base()
          {
               this.text = "Not assigned.";
               this.color = Color.White;
               this.fontKey = "Default";
          }

          /// <summary>
          /// Create a text.
          /// </summary>
          /// <param name="text">The text to display.</param>
          public Text(string text)
               : base()
          {
               this.text = text;
               this.color = Color.White;
               this.fontKey = "Default";
          }

          /// <summary>
          /// Create a text.
          /// </summary>
          /// <param name="text">The text to display.</param>
          /// <param name="color">The color to set the text.</param>
          public Text(string text, Color color)
               : base()
          {
               this.text = text;
               this.color = color;
               this.fontKey = "Default";
          }

          public Text(string name, string text, Color color)
              : base()
          {
              this.text = text;
              this.color = color;
              this.fontKey = "Default";
          }

          /// <summary>
          /// Create a text.
          /// </summary>
          /// <param name="text">The text to display.</param>
          /// <param name="color">The color to set the text.</param>
          /// <param name="parent">The parent of the text.</param>
          public Text(string text, Color color, Element parent)
               : base(parent)
          {
               this.text = text;
               this.color = color;
               this.fontKey = "Default";
          }

          /// <summary>
          /// Create a text.
          /// </summary>
          /// <param name="text">The text to display.</param>
          /// <param name="color">The color to set the text.</param>
          /// <param name="position">The position to offset from the parent.</param>
          /// <param name="parent">The parent.</param>
          public Text(string text, Color color, Vector2 position, Element parent)
               : base(parent, position)
          {
               this.text = text;
               this.color = color;
               this.fontKey = "Default";
          }

          /// <summary>
          /// Create a text.
          /// </summary>
          /// <param name="text">The text to display.</param>
          /// <param name="color">The color to set the text.</param>
          /// <param name="fontKey">The font to set the text.</param>
          public Text(string text, Color color, string fontKey)
               : base()
          {
               this.text = text;
               this.color = color;
               this.fontKey = fontKey;
          }

          /// <summary>
          /// Create a text.
          /// </summary>
          /// <param name="text">The text to display.</param>
          /// <param name="color">The color to set the text.</param>
          /// <param name="fontKey">The font to set the text.</param>
          /// <param name="parent">The parent of the text.</param>
          public Text(string text, Color color, string fontKey, Element parent)
               : base(parent)
          {
               this.text = text;
               this.color = color;
               this.fontKey = fontKey;
          }

          /// <summary>
          /// Create a text.
          /// </summary>
          /// <param name="text">The string to display.</param>
          /// <param name="color">The color to set the text.</param>
          /// <param name="fontKey">The font to use.</param>
          /// <param name="position">The position to offset from the parent.</param>
          /// <param name="parent">The parent.</param>
          public Text(string text, Color color, string fontKey, Vector2 position, Element parent)
               : base(parent, position)
          {
               this.text = text;
               this.color = color;
               this.fontKey = fontKey;
          }

          public override void Load()
          {
              if (this.Parent != null)
              {
                  if (this.name != null)
                      this.name = Parent.Name + this.name;
                  else
                      this.name = Parent.Name + "Text";
              }

               Utilities.ContentLibrary.LoadFont(fontKey, fontKey);
               this.bounds = new Rectangle((int)this.Position.X, (int)this.Position.Y,
                    (int)Utilities.ContentLibrary.GetFont(this.fontKey).MeasureString(this.text).X,
                    (int)Utilities.ContentLibrary.GetFont(this.fontKey).MeasureString(this.text).Y);
               base.Load();
          }

          public override void Draw(GameTime gameTime, Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
          {
               spriteBatch.DrawString(Utilities.ContentLibrary.GetFont(fontKey), text, this.Position, this.color);
               base.Draw(gameTime, spriteBatch);
          }
     }
}
